package com.pignest.hotel.ajax;

import java.util.Date;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

import com.pignest.hotel.beans.Dictionary;
import com.pignest.hotel.schedule.NightAuditJob;
import com.pignest.hotel.service.DictionaryService;
import com.pignest.hotel.spring.Services;
import com.pignest.hotel.system.Config;
import com.pignest.hotel.system.Key;
import com.pignest.hotel.system.Permission;
import com.pignest.hotel.util.DateUtil;

@RemoteProxy
public class AdminAjax extends HotelAjax {
	
	@RemoteMethod
	public String getLastNightAuditInfo() {
		DictionaryService dictionaryService = Services.getDictionaryService();
		Dictionary lastAuditTime = dictionaryService.getDictionaryByKey(Key.LAST_AUDIT_TIME);
		return lastAuditTime != null ? lastAuditTime.getValue() : "暂未夜审";
	}
	
	@RemoteMethod
	public String nightAudit() {
		checkPermission(Permission.ADMINISTER);
		DictionaryService dictionaryService = Services.getDictionaryService();
		Dictionary lastAuditTime = dictionaryService.getDictionaryByKey(Key.LAST_AUDIT_TIME);
		Date time = lastAuditTime == null ? null : DateUtil.toDate(lastAuditTime.getValue());
		if (time != null) {
			int h = Config.getMinNightAuditInterval();
			if (System.currentTimeMillis() - time.getTime() < h * DateUtil.ONE_HOUR)
				return "上次夜审发生在 " + lastAuditTime.getValue() + ", 暂时无法夜审.";
		}
		new NightAuditJob().audit();
		return null;
	}
	
}
